﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Threading;
using System.Configuration;

namespace BNet_Bot
{
    public class DataBuffer
    {
        public MemoryStream mStream;

        public DataBuffer()
        {
            mStream = new MemoryStream();
        }

        public void Dispose()
        {
            mStream.Dispose();
        }

        public virtual byte[] GetBuffer()
        {
            return mStream.GetBuffer();
        }

        public void AddDWORD(string str)
        {
            if (str.Length > 4)
                throw new ArgumentException("Max DWORD Length of 4 was exceeded.");

            lock (this)
            {
                if (str.Length < 4)
                {
                    int numNulls = 4 - str.Length;
                    for (int i = 0; i < numNulls; i++)
                        AddByte(0);
                    byte[] tempB = Encoding.ASCII.GetBytes(str);
                    for (int i = tempB.Length - 1; i >= 0; i--)
                        AddByte(tempB[i]);
                }
            }
        }

        public void AddByte(byte b)
        {
            lock (this)
            {
                mStream.WriteByte(b);
            }
        }


        public void AddInt16(short s)
        {
            lock (this)
            {
                mStream.Write(BitConverter.GetBytes(s), 0, 2);
            }
        }

        public void AddInt32(int i)
        {
            lock (this)
            {
                mStream.Write(BitConverter.GetBytes(i), 0, 4);
            }
        }

        public void AddByteArray(byte[] b)
        {
            lock (this)
            {
                mStream.Write(b, 0, b.Length);
            }
        }

        public void AddNTString(string str)
        {
            lock (this)
            {
                byte[] tmp = System.Text.Encoding.UTF8.GetBytes(str);
                mStream.Write(tmp, 0, tmp.Length);
                mStream.WriteByte(0x00);
            }
        }

        
    }
}
